lib: Add a helper for mmap->bytes with openat(), use it in repo
authorColin Walters <walters@verbum.org>
Fri, 3 Jun 2016 15:00:10 +0000 (11:00 -0400)
committerAtomic Bot <atomic-devel@projectatomic.io>
Thu, 9 Jun 2016 14:39:09 +0000 (14:39 +0000)
This kills another GSystem consumer...I think down the line I'd like
to do something like "detect whether file is > 1k if so, mmap,
otherwise just readall()" so we can use this helper in more places.

Closes: #319
Approved by: jlebon

src/libostree/ostree-repo.c
src/libotutil/ot-fs-utils.c
src/libotutil/ot-fs-utils.h

index 2a26ffedd082ab8ae4d2a5288fb3d1eeece6f8bf..5c4e941eede65a5fc805f4072e9c777a76f8c4a6 100644 (file)
@@ -4618,7 +4618,6 @@ ostree_repo_add_gpg_signature_summary (OstreeRepo     *self,
 {
   gboolean ret = FALSE;
   g_autoptr(GBytes) summary_data = NULL;
-  g_autoptr(GFile) summary_file = NULL;
   g_autoptr(GFile) signature_path = NULL;
   g_autoptr(GVariant) existing_signatures = NULL;
   g_autoptr(GVariant) new_metadata = NULL;
@@ -4626,8 +4625,7 @@ ostree_repo_add_gpg_signature_summary (OstreeRepo     *self,
   guint i;
   signature_path = g_file_resolve_relative_path (self->repodir, "summary.sig");
 
-  summary_file = g_file_resolve_relative_path (self->repodir, "summary");
-  summary_data = gs_file_map_readonly (summary_file, cancellable, error);
+  summary_data = ot_file_mapat_bytes (self->repo_dir_fd, "summary", error);
   if (!summary_data)
     goto out;
 
index 4d45cd060f27735bd6b11d316cc2d8187eed0a83..46a0405b8302de09a2b6aa4640f9efa5bb169568 100644 (file)
@@ -230,3 +230,24 @@ ot_openat_ignore_enoent (int dfd,
  out:
   return ret;
 }
+
+GBytes *
+ot_file_mapat_bytes (int dfd,
+                     const char *path,
+                     GError **error)
+{
+  glnx_fd_close int fd = openat (dfd, path, O_RDONLY | O_CLOEXEC);
+  g_autoptr(GMappedFile) mfile = NULL;
+
+  if (fd < 0)
+    {
+      glnx_set_error_from_errno (error);
+      return FALSE;
+    }
+
+  mfile = g_mapped_file_new_from_fd (fd, FALSE, error);
+  if (!mfile)
+    return FALSE;
+
+  return g_mapped_file_get_bytes (mfile);
+}
index cfeea74d42d830d85e861d7c0f78d1282cec8203..27f0f38ec60d4e181739236a1d05c8a1c8a92d9d 100644 (file)
@@ -66,4 +66,8 @@ gboolean ot_openat_ignore_enoent (int dfd,
                                   int *out_fd,
                                   GError **error);
 
+GBytes *ot_file_mapat_bytes (int dfd,
+                             const char *path,
+                             GError **error);
+
 G_END_DECLS